<!DOCTYPE html>
<html lang="en-US">
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="language" content="en" />
        <link href="./assets/ffd55088/css/bootstrap.css" rel="stylesheet">
<link href="./assets/5cf9384a/solarized_light.css" rel="stylesheet">
<link href="./assets/6c54116e/style.css" rel="stylesheet">
<script src="./assets/a44cef0f/jquery.js"></script>
<script src="./assets/ffd55088/js/bootstrap.js"></script>
<script src="./assets/8ac4e28a/jssearch.js"></script>    <title>Controllers - RESTful Web Services - The Definitive Guide to Yii 2.0</title>
</head>
<body>

<div class="wrap">
    <nav id="w187" class="navbar-inverse navbar-fixed-top navbar" role="navigation"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#w187-collapse"><span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span></button><a class="navbar-brand" href="./index.html">The Definitive Guide to Yii 2.0</a></div><div id="w187-collapse" class="collapse navbar-collapse"><ul id="w188" class="navbar-nav nav"><li><a href="./index.html">Class reference</a></li>
<li class="dropdown"><a class="dropdown-toggle" href="#" data-toggle="dropdown">Extensions <b class="caret"></b></a><ul id="w189" class="dropdown-menu"><li><a href="./ext-apidoc-index.html" tabindex="-1">apidoc</a></li>
<li><a href="./ext-authclient-index.html" tabindex="-1">authclient</a></li>
<li><a href="./ext-bootstrap-index.html" tabindex="-1">bootstrap</a></li>
<li><a href="./ext-codeception-index.html" tabindex="-1">codeception</a></li>
<li><a href="./ext-debug-index.html" tabindex="-1">debug</a></li>
<li><a href="./ext-elasticsearch-index.html" tabindex="-1">elasticsearch</a></li>
<li><a href="./ext-faker-index.html" tabindex="-1">faker</a></li>
<li><a href="./ext-gii-index.html" tabindex="-1">gii</a></li>
<li><a href="./ext-imagine-index.html" tabindex="-1">imagine</a></li>
<li><a href="./ext-jui-index.html" tabindex="-1">jui</a></li>
<li><a href="./ext-mongodb-index.html" tabindex="-1">mongodb</a></li>
<li><a href="./ext-redis-index.html" tabindex="-1">redis</a></li>
<li><a href="./ext-smarty-index.html" tabindex="-1">smarty</a></li>
<li><a href="./ext-sphinx-index.html" tabindex="-1">sphinx</a></li>
<li><a href="./ext-swiftmailer-index.html" tabindex="-1">swiftmailer</a></li>
<li><a href="./ext-twig-index.html" tabindex="-1">twig</a></li></ul></li>
<li><a href="./guide-README.html">Guide</a></li></ul><div class="navbar-form navbar-left" role="search">
  <div class="form-group">
    <input id="searchbox" type="text" class="form-control" placeholder="Search">
  </div>
</div>
</div></nav>
    <div id="search-resultbox" style="display: none;" class="modal-content">
        <ul id="search-results">
        </ul>
    </div>

    
<div class="row">
    <div class="col-md-2">
                <div id="navigation" class="list-group"><a class="list-group-item" href="#navigation-171" data-toggle="collapse" data-parent="#navigation">Introduction <b class="caret"></b></a><div id="navigation-171" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-intro-yii.html">About Yii</a>
<a class="list-group-item" href="./guide-intro-upgrade-from-v1.html">Upgrading from Version 1.1</a></div>
<a class="list-group-item" href="#navigation-172" data-toggle="collapse" data-parent="#navigation">Getting Started <b class="caret"></b></a><div id="navigation-172" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-start-installation.html">Installing Yii</a>
<a class="list-group-item" href="./guide-start-workflow.html">Running Applications</a>
<a class="list-group-item" href="./guide-start-hello.html">Saying Hello</a>
<a class="list-group-item" href="./guide-start-forms.html">Working with Forms</a>
<a class="list-group-item" href="./guide-start-databases.html">Working with Databases</a>
<a class="list-group-item" href="./guide-start-gii.html">Generating Code with Gii</a>
<a class="list-group-item" href="./guide-start-looking-ahead.html">Looking Ahead</a></div>
<a class="list-group-item" href="#navigation-173" data-toggle="collapse" data-parent="#navigation">Application Structure <b class="caret"></b></a><div id="navigation-173" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-structure-overview.html">Overview</a>
<a class="list-group-item" href="./guide-structure-entry-scripts.html">Entry Scripts</a>
<a class="list-group-item" href="./guide-structure-applications.html">Applications</a>
<a class="list-group-item" href="./guide-structure-application-components.html">Application Components</a>
<a class="list-group-item" href="./guide-structure-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-structure-models.html">Models</a>
<a class="list-group-item" href="./guide-structure-views.html">Views</a>
<a class="list-group-item" href="./guide-structure-modules.html">Modules</a>
<a class="list-group-item" href="./guide-structure-filters.html">Filters</a>
<a class="list-group-item" href="./guide-structure-widgets.html">Widgets</a>
<a class="list-group-item" href="./guide-structure-assets.html">Assets</a>
<a class="list-group-item" href="./guide-structure-extensions.html">Extensions</a></div>
<a class="list-group-item" href="#navigation-174" data-toggle="collapse" data-parent="#navigation">Handling Requests <b class="caret"></b></a><div id="navigation-174" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-runtime-overview.html">Overview</a>
<a class="list-group-item" href="./guide-runtime-bootstrapping.html">Bootstrapping</a>
<a class="list-group-item" href="./guide-runtime-routing.html">Routing and URL Creation</a>
<a class="list-group-item" href="./guide-runtime-requests.html">Requests</a>
<a class="list-group-item" href="./guide-runtime-responses.html">Responses</a>
<a class="list-group-item" href="./guide-runtime-sessions-cookies.html">Sessions and Cookies</a>
<a class="list-group-item" href="./guide-runtime-handling-errors.html">Handling Errors</a>
<a class="list-group-item" href="./guide-runtime-logging.html">Logging</a></div>
<a class="list-group-item" href="#navigation-175" data-toggle="collapse" data-parent="#navigation">Key Concepts <b class="caret"></b></a><div id="navigation-175" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-concept-components.html">Components</a>
<a class="list-group-item" href="./guide-concept-properties.html">Properties</a>
<a class="list-group-item" href="./guide-concept-events.html">Events</a>
<a class="list-group-item" href="./guide-concept-behaviors.html">Behaviors</a>
<a class="list-group-item" href="./guide-concept-configurations.html">Configurations</a>
<a class="list-group-item" href="./guide-concept-aliases.html">Aliases</a>
<a class="list-group-item" href="./guide-concept-autoloading.html">Class Autoloading</a>
<a class="list-group-item" href="./guide-concept-service-locator.html">Service Locator</a>
<a class="list-group-item" href="./guide-concept-di-container.html">Dependency Injection Container</a></div>
<a class="list-group-item" href="#navigation-176" data-toggle="collapse" data-parent="#navigation">Working with Databases <b class="caret"></b></a><div id="navigation-176" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-db-dao.html">Data Access Objects</a>
<a class="list-group-item" href="./guide-db-query-builder.html">Query Builder</a>
<a class="list-group-item" href="./guide-db-active-record.html">Active Record</a>
<a class="list-group-item" href="./guide-db-migrations.html">Migrations</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-sphinx/blob/master/docs/guide/README.md">Sphinx</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-redis/blob/master/docs/guide/README.md">Redis</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-mongodb/blob/master/docs/guide/README.md">MongoDB</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-elasticsearch/blob/master/docs/guide/README.md">ElasticSearch</a></div>
<a class="list-group-item" href="#navigation-177" data-toggle="collapse" data-parent="#navigation">Getting Data from Users <b class="caret"></b></a><div id="navigation-177" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-input-forms.html">Creating Forms</a>
<a class="list-group-item" href="./guide-input-validation.html">Validating Input</a>
<a class="list-group-item" href="./guide-input-file-upload.html">Uploading Files</a>
<a class="list-group-item" href="./guide-input-tabular-input.html">Collecting Tabular Input</a>
<a class="list-group-item" href="./guide-input-multiple-models.html">Getting Data for Multiple Models</a></div>
<a class="list-group-item" href="#navigation-178" data-toggle="collapse" data-parent="#navigation">Displaying Data <b class="caret"></b></a><div id="navigation-178" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-output-formatting.html">Data Formatting</a>
<a class="list-group-item" href="./guide-output-pagination.html">Pagination</a>
<a class="list-group-item" href="./guide-output-sorting.html">Sorting</a>
<a class="list-group-item" href="./guide-output-data-providers.html">Data Providers</a>
<a class="list-group-item" href="./guide-output-data-widgets.html">Data Widgets</a>
<a class="list-group-item" href="./guide-output-client-scripts.html">Working with Client Scripts</a>
<a class="list-group-item" href="./guide-output-theming.html">Theming</a></div>
<a class="list-group-item" href="#navigation-179" data-toggle="collapse" data-parent="#navigation">Security <b class="caret"></b></a><div id="navigation-179" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-security-overview.html">Overview</a>
<a class="list-group-item" href="./guide-security-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-security-authorization.html">Authorization</a>
<a class="list-group-item" href="./guide-security-passwords.html">Working with Passwords</a>
<a class="list-group-item" href="./guide-security-cryptography.html">Cryptography</a>
<a class="list-group-item" href="./guide-structure-views.html#security">Views security</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-authclient/blob/master/docs/guide/README.md">Auth Clients</a>
<a class="list-group-item" href="./guide-security-best-practices.html">Best Practices</a></div>
<a class="list-group-item" href="#navigation-180" data-toggle="collapse" data-parent="#navigation">Caching <b class="caret"></b></a><div id="navigation-180" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-caching-overview.html">Overview</a>
<a class="list-group-item" href="./guide-caching-data.html">Data Caching</a>
<a class="list-group-item" href="./guide-caching-fragment.html">Fragment Caching</a>
<a class="list-group-item" href="./guide-caching-page.html">Page Caching</a>
<a class="list-group-item" href="./guide-caching-http.html">HTTP Caching</a></div>
<a class="list-group-item active" href="#navigation-181" data-toggle="collapse" data-parent="#navigation">RESTful Web Services <b class="caret"></b></a><div id="navigation-181" class="submenu panel-collapse collapse in"><a class="list-group-item" href="./guide-rest-quick-start.html">Quick Start</a>
<a class="list-group-item" href="./guide-rest-resources.html">Resources</a>
<a class="list-group-item active" href="./guide-rest-controllers.html">Controllers</a>
<a class="list-group-item" href="./guide-rest-routing.html">Routing</a>
<a class="list-group-item" href="./guide-rest-response-formatting.html">Response Formatting</a>
<a class="list-group-item" href="./guide-rest-authentication.html">Authentication</a>
<a class="list-group-item" href="./guide-rest-rate-limiting.html">Rate Limiting</a>
<a class="list-group-item" href="./guide-rest-versioning.html">Versioning</a>
<a class="list-group-item" href="./guide-rest-error-handling.html">Error Handling</a></div>
<a class="list-group-item" href="#navigation-182" data-toggle="collapse" data-parent="#navigation">Development Tools <b class="caret"></b></a><div id="navigation-182" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-debug/blob/master/docs/guide/README.md">Debug Toolbar and Debugger</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/README.md">Generating Code using Gii</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-apidoc">Generating API Documentation</a></div>
<a class="list-group-item" href="#navigation-183" data-toggle="collapse" data-parent="#navigation">Testing <b class="caret"></b></a><div id="navigation-183" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-test-overview.html">Overview</a>
<a class="list-group-item" href="./guide-test-environment-setup.html">Testing environment setup</a>
<a class="list-group-item" href="./guide-test-unit.html">Unit Tests</a>
<a class="list-group-item" href="./guide-test-functional.html">Functional Tests</a>
<a class="list-group-item" href="./guide-test-acceptance.html">Acceptance Tests</a>
<a class="list-group-item" href="./guide-test-fixtures.html">Fixtures</a></div>
<a class="list-group-item" href="#navigation-184" data-toggle="collapse" data-parent="#navigation">Special Topics <b class="caret"></b></a><div id="navigation-184" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-app-advanced/blob/master/docs/guide/README.md">Advanced Project Template</a>
<a class="list-group-item" href="./guide-tutorial-start-from-scratch.html">Building Application from Scratch</a>
<a class="list-group-item" href="./guide-tutorial-console.html">Console Commands</a>
<a class="list-group-item" href="./guide-tutorial-core-validators.html">Core Validators</a>
<a class="list-group-item" href="./guide-tutorial-i18n.html">Internationalization</a>
<a class="list-group-item" href="./guide-tutorial-mailing.html">Mailing</a>
<a class="list-group-item" href="./guide-tutorial-performance-tuning.html">Performance Tuning</a>
<a class="list-group-item" href="./guide-tutorial-shared-hosting.html">Shared Hosting Environment</a>
<a class="list-group-item" href="./guide-tutorial-template-engines.html">Template Engines</a>
<a class="list-group-item" href="./guide-tutorial-yii-integration.html">Working with Third-Party Code</a></div>
<a class="list-group-item" href="#navigation-185" data-toggle="collapse" data-parent="#navigation">Widgets <b class="caret"></b></a><div id="navigation-185" class="submenu panel-collapse collapse"><a class="list-group-item" href="https://github.com/yiisoft/yii2-bootstrap/blob/master/docs/guide/README.md">Bootstrap Widgets</a>
<a class="list-group-item" href="https://github.com/yiisoft/yii2-jui/blob/master/docs/guide/README.md">jQuery UI Widgets</a></div>
<a class="list-group-item" href="#navigation-186" data-toggle="collapse" data-parent="#navigation">Helpers <b class="caret"></b></a><div id="navigation-186" class="submenu panel-collapse collapse"><a class="list-group-item" href="./guide-helper-overview.html">Overview</a>
<a class="list-group-item" href="./guide-helper-array.html">ArrayHelper</a>
<a class="list-group-item" href="./guide-helper-html.html">Html</a>
<a class="list-group-item" href="./guide-helper-url.html">Url</a></div></div>    </div>
    <div class="col-md-9 guide-content" role="main">
        <h1>Controllers <span id="controllers"></span><a href="#controllers" class="hashlink">&para;</a></h1>
<div class="toc"><ol><li><a href="#creating-controller">Creating Controller Classes</a></li>
<li><a href="#filters">Filters</a></li>
<li><a href="#extending-active-controller">Extending ActiveController</a></li></ol></div>
<p>After creating the resource classes and specifying how resource data should be formatted, the next thing
to do is to create controller actions to expose the resources to end users through RESTful APIs.</p>
<p>Yii provides two base controller classes to simplify your work of creating RESTful actions:
<a href="./yii-rest-controller.html">yii\rest\Controller</a> and <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a>. The difference between these two controllers
is that the latter provides a default set of actions that are specifically designed to deal with
resources represented as <a href="guide-db-active-record.html">Active Record</a>. So if you are using <a href="guide-db-active-record.html">Active Record</a>
and are comfortable with the provided built-in actions, you may consider extending your controller classes
from <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a>, which will allow you to create powerful RESTful APIs with minimal code.</p>
<p>Both <a href="./yii-rest-controller.html">yii\rest\Controller</a> and <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a> provide the following features, some of which
will be described in detail in the next few sections:</p>
<ul>
<li>HTTP method validation;</li>
<li><a href="guide-rest-response-formatting.html">Content negotiation and Data formatting</a>;</li>
<li><a href="guide-rest-authentication.html">Authentication</a>;</li>
<li><a href="guide-rest-rate-limiting.html">Rate limiting</a>.</li>
</ul>
<p><a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a> in addition provides the following features:</p>
<ul>
<li>A set of commonly needed actions: <code>index</code>, <code>view</code>, <code>create</code>, <code>update</code>, <code>delete</code>, <code>options</code>;</li>
<li>User authorization in regard to the requested action and resource.</li>
</ul>
<h2>Creating Controller Classes  <span id="creating-controller"></span><a href="#creating-controller" class="hashlink">&para;</a></h2><p>When creating a new controller class, a convention in naming the controller class is to use
the type name of the resource and use singular form. For example, to serve user information,
the controller may be named as <code>UserController</code>.</p>
<p>Creating a new action is similar to creating an action for a Web application. The only difference
is that instead of rendering the result using a view by calling the <code>render()</code> method, for RESTful actions
you directly return the data. The <a href="./yii-rest-controller.html#$serializer-detail">serializer</a> and the
<a href="./yii-web-response.html">response object</a> will handle the conversion from the original data to the requested
format. For example,</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actionView</span><span class="hljs-params">(<span class="hljs-variable">$id</span>)</span>
</span>{
    <span class="hljs-keyword">return</span> User::findOne(<span class="hljs-variable">$id</span>);
}
</code></pre>
<h2>Filters  <span id="filters"></span><a href="#filters" class="hashlink">&para;</a></h2><p>Most RESTful API features provided by <a href="./yii-rest-controller.html">yii\rest\Controller</a> are implemented in terms of <a href="guide-structure-filters.html">filters</a>.
In particular, the following filters will be executed in the order they are listed:</p>
<ul>
<li><a href="./yii-filters-contentnegotiator.html">contentNegotiator</a>: supports content negotiation, to be explained in
the <a href="guide-rest-response-formatting.html">Response Formatting</a> section;</li>
<li><a href="./yii-filters-verbfilter.html">verbFilter</a>: supports HTTP method validation;</li>
<li><a href="./yii-filters-auth-authmethod.html">authenticator</a>: supports user authentication, to be explained in
the <a href="guide-rest-authentication.html">Authentication</a> section;</li>
<li><a href="./yii-filters-ratelimiter.html">rateLimiter</a>: supports rate limiting, to be explained in
the <a href="guide-rest-rate-limiting.html">Rate Limiting</a> section.</li>
</ul>
<p>These named filters are declared in the <a href="./yii-rest-controller.html#behaviors()-detail">behaviors()</a> method.
You may override this method to configure individual filters, disable some of them, or add your own filters.
For example, if you only want to use HTTP basic authentication, you may write the following code:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">filters</span>\<span class="hljs-title">auth</span>\<span class="hljs-title">HttpBasicAuth</span>;

<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">behaviors</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-variable">$behaviors</span> = <span class="hljs-keyword">parent</span>::behaviors();
    <span class="hljs-variable">$behaviors</span>[<span class="hljs-string">'authenticator'</span>] = [
        <span class="hljs-string">'class'</span> =&gt; HttpBasicAuth::className(),
    ];
    <span class="hljs-keyword">return</span> <span class="hljs-variable">$behaviors</span>;
}
</code></pre>
<h3>CORS  <span id="cors"></span><a href="#cors" class="hashlink">&para;</a></h3><p>Adding the <a href="guide-structure-filters.html#cors">Cross Origin Resource Sharing</a> filter to a controller is a bit more complicated
than adding other filters described above, because the CORS filter has to be applied before authentication methods
and thus needs a slightly different approach compared to other filters. Also authentication has to be disabled for the
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests">CORS Preflight requests</a>
so that a browser can safely determine whether a request can be made beforehand without the need for sending
authentication credentials. The follow code shows the code that is needed to add the <a href="./yii-filters-cors.html">yii\filters\Cors</a> filter
to an existing controller that extends from <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a>:</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">use</span> <span class="hljs-title">yii</span>\<span class="hljs-title">filters</span>\<span class="hljs-title">auth</span>\<span class="hljs-title">HttpBasicAuth</span>;

<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">behaviors</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-variable">$behaviors</span> = <span class="hljs-keyword">parent</span>::behaviors();

    <span class="hljs-comment">// remove authentication filter</span>
    <span class="hljs-variable">$auth</span> = <span class="hljs-variable">$behaviors</span>[<span class="hljs-string">'authenticator'</span>];
    <span class="hljs-keyword">unset</span>(<span class="hljs-variable">$behaviors</span>[<span class="hljs-string">'authenticator'</span>]);
    
    <span class="hljs-comment">// add CORS filter</span>
    <span class="hljs-variable">$behaviors</span>[<span class="hljs-string">'corsFilter'</span>] = [
        <span class="hljs-string">'class'</span> =&gt; \yii\filters\Cors::className(),
    ];
    
    <span class="hljs-comment">// re-add authentication filter</span>
    <span class="hljs-variable">$behaviors</span>[<span class="hljs-string">'authenticator'</span>] = <span class="hljs-variable">$auth</span>;
    <span class="hljs-comment">// avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method)</span>
    <span class="hljs-variable">$behaviors</span>[<span class="hljs-string">'authenticator'</span>][<span class="hljs-string">'except'</span>] = [<span class="hljs-string">'options'</span>];

    <span class="hljs-keyword">return</span> <span class="hljs-variable">$behaviors</span>;
}
</code></pre>
<h2>Extending <code>ActiveController</code>  <span id="extending-active-controller"></span><a href="#extending-active-controller" class="hashlink">&para;</a></h2><p>If your controller class extends from <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a>, you should set
its <a href="./yii-rest-activecontroller.html#$modelClass-detail">modelClass</a> property to be the name of the resource class
that you plan to serve through this controller. The class must extend from <a href="./yii-db-activerecord.html">yii\db\ActiveRecord</a>.</p>
<h3>Customizing Actions  <span id="customizing-actions"></span><a href="#customizing-actions" class="hashlink">&para;</a></h3><p>By default, <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a> provides the following actions:</p>
<ul>
<li><a href="./yii-rest-indexaction.html">index</a>: list resources page by page;</li>
<li><a href="./yii-rest-viewaction.html">view</a>: return the details of a specified resource;</li>
<li><a href="./yii-rest-createaction.html">create</a>: create a new resource;</li>
<li><a href="./yii-rest-updateaction.html">update</a>: update an existing resource;</li>
<li><a href="./yii-rest-deleteaction.html">delete</a>: delete the specified resource;</li>
<li><a href="./yii-rest-optionsaction.html">options</a>: return the supported HTTP methods.</li>
</ul>
<p>All these actions are declared through the <a href="./yii-rest-activecontroller.html#actions()-detail">actions()</a> method.
You may configure these actions or disable some of them by overriding the <code>actions()</code> method, like shown the following,</p>
<pre><code class="hljs php language-php"><span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">actions</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-variable">$actions</span> = <span class="hljs-keyword">parent</span>::actions();

    <span class="hljs-comment">// disable the "delete" and "create" actions</span>
    <span class="hljs-keyword">unset</span>(<span class="hljs-variable">$actions</span>[<span class="hljs-string">'delete'</span>], <span class="hljs-variable">$actions</span>[<span class="hljs-string">'create'</span>]);

    <span class="hljs-comment">// customize the data provider preparation with the "prepareDataProvider()" method</span>
    <span class="hljs-variable">$actions</span>[<span class="hljs-string">'index'</span>][<span class="hljs-string">'prepareDataProvider'</span>] = [<span class="hljs-variable">$this</span>, <span class="hljs-string">'prepareDataProvider'</span>];

    <span class="hljs-keyword">return</span> <span class="hljs-variable">$actions</span>;
}

<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">prepareDataProvider</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">// prepare and return a data provider for the "index" action</span>
}
</code></pre>
<p>Please refer to the class references for individual action classes to learn what configuration options are available.</p>
<h3>Performing Access Check  <span id="performing-access-check"></span><a href="#performing-access-check" class="hashlink">&para;</a></h3><p>When exposing resources through RESTful APIs, you often need to check if the current user has the permission
to access and manipulate the requested resource(s). With <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a>, this can be done
by overriding the <a href="./yii-rest-activecontroller.html#checkAccess()-detail">checkAccess()</a> method like the following,</p>
<pre><code class="hljs php language-php"><span class="hljs-comment">/**
 * Checks the privilege of the current user.
 *
 * This method should be overridden to check whether the current user has the privilege
 * to run the specified action against the specified data model.
 * If the user does not have access, a [[ForbiddenHttpException]] should be thrown.
 *
 * <span class="hljs-doctag">@param</span> string $action the ID of the action to be executed
 * <span class="hljs-doctag">@param</span> \yii\base\Model $model the model to be accessed. If null, it means no specific model is being accessed.
 * <span class="hljs-doctag">@param</span> array $params additional parameters
 * <span class="hljs-doctag">@throws</span> ForbiddenHttpException if the user does not have access
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkAccess</span><span class="hljs-params">(<span class="hljs-variable">$action</span>, <span class="hljs-variable">$model</span> = null, <span class="hljs-variable">$params</span> = [])</span>
</span>{
    <span class="hljs-comment">// check if the user can access $action and $model</span>
    <span class="hljs-comment">// throw ForbiddenHttpException if access should be denied</span>
}
</code></pre>
<p>The <code>checkAccess()</code> method will be called by the default actions of <a href="./yii-rest-activecontroller.html">yii\rest\ActiveController</a>. If you create
new actions and also want to perform access check, you should call this method explicitly in the new actions.</p>
<blockquote class="tip"><p><strong>Tip: </strong>You may implement <code>checkAccess()</code> by using the <a href="guide-security-authorization.html">Role-Based Access Control (RBAC) component</a>.</p>
</blockquote>
        <div class="toplink"><a href="#" class="h1" title="go to top"><span class="glyphicon glyphicon-arrow-up"></a></div>
    </div>
</div>


</div>

<footer class="footer">
        <p class="pull-right"><small>Page generated on Sat, 09 Jul 2016 12:16:28 +0000</small></p>
    Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a></footer>

<script type="text/javascript">jQuery(document).ready(function () {
    var shiftWindow = function () { scrollBy(0, -50) };
    if (location.hash) setTimeout(shiftWindow, 1);
    window.addEventListener("hashchange", shiftWindow);
var element = document.createElement("script");
element.src = "./jssearch.index.js";
document.body.appendChild(element);

var searchBox = $('#searchbox');

// search when typing in search field
searchBox.on("keyup", function(event) {
    var query = $(this).val();

    if (query == '' || event.which == 27) {
        $('#search-resultbox').hide();
        return;
    } else if (event.which == 13) {
        var selectedLink = $('#search-resultbox a.selected');
        if (selectedLink.length != 0) {
            document.location = selectedLink.attr('href');
            return;
        }
    } else if (event.which == 38 || event.which == 40) {
        $('#search-resultbox').show();

        var selected = $('#search-resultbox a.selected');
        if (selected.length == 0) {
            $('#search-results').find('a').first().addClass('selected');
        } else {
            var next;
            if (event.which == 40) {
                next = selected.parent().next().find('a').first();
            } else {
                next = selected.parent().prev().find('a').first();
            }
            if (next.length != 0) {
                var resultbox = $('#search-results');
                var position = next.position();

//              TODO scrolling is buggy and jumps around
//                resultbox.scrollTop(Math.floor(position.top));
//                console.log(position.top);

                selected.removeClass('selected');
                next.addClass('selected');
            }
        }

        return;
    }
    $('#search-resultbox').show();
    $('#search-results').html('<li><span class="no-results">No results</span></li>');

    var result = jssearch.search(query);

    if (result.length > 0) {
        var i = 0;
        var resHtml = '';

        for (var key in result) {
            if (i++ > 20) {
                break;
            }
            resHtml = resHtml +
            '<li><a href="' + result[key].file.u.substr(3) +'"><span class="title">' + result[key].file.t + '</span>' +
            '<span class="description">' + result[key].file.d + '</span></a></li>';
        }
        $('#search-results').html(resHtml);
    }
});

// hide the search results on ESC
$(document).on("keyup", function(event) { if (event.which == 27) { $('#search-resultbox').hide(); } });
// hide search results on click to document
$(document).bind('click', function (e) { $('#search-resultbox').hide(); });
// except the following:
searchBox.bind('click', function(e) { e.stopPropagation(); });
$('#search-resultbox').bind('click', function(e) { e.stopPropagation(); });

});</script></body>
</html>
